package com.example;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class SelectedCourseDAO {

    public List<SelectedCourse> getSelectedCoursesByStudentUsername(String studentUsername) {
        List<SelectedCourse> selectedCourses = new ArrayList<>();
        String sql = "SELECT id, studentUsername, courseName, credits, semester FROM selected_courses WHERE studentUsername = ?";

        try (Connection conn = DatabaseUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {

            pstmt.setString(1, studentUsername);
            ResultSet rs = pstmt.executeQuery();

            while (rs.next()) {
                SelectedCourse course = new SelectedCourse(
                    rs.getInt("id"),
                    rs.getString("studentUsername"),
                    rs.getString("courseName"),
                    rs.getDouble("credits"),
                    rs.getString("semester")
                );
                selectedCourses.add(course);
            }
        } catch (SQLException e) {
            System.err.println("Error fetching selected courses for student " + studentUsername + ": " + e.getMessage());
        }
        return selectedCourses;
    }
} 